﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MealBooking.Core.Models.Meals;
using MealBooking.Core.Models.Accounts;

namespace MealBooking.Core.Models.Orders
{
    public class Order
    {
        public string Id { get; set; }
        public string ManagerOrderId { get; set; }
        public Account Account { get; set; }
        public Manager Manager { get; set; }
        public DateTime CreatedTime { get; set; }
        public DateTime LastUpdateTime { get; set; }
        /// <summary>
        /// 账单总价
        /// </summary>
        public double TotalPrice
        {
            get
            {
                if (MealsWithSum != null && MealsWithSum.Count != 0)
                {
                    return MealsWithSum.Sum(t => t.MealSum * t.Meal.Price);
                }
                return 0;
            }
            //set
            //{
            //    value = TotalPrice;
            //}
        }
        /// <summary>
        /// 是否已下单
        /// </summary>
        public bool IsOrdered { get; set; }
        public DateTime OrderedTime { get; set; }
        /// <summary>
        /// 是否已订购
        /// </summary>
        public bool IsBooked { get; set; }
        public DateTime BookedTime { get; set; }
        public List<MealWithSum> MealsWithSum { get; set; }
        public string Comment { get; set; }

        /// <summary>
        /// meal with templates details
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            if (MealsWithSum != null && MealsWithSum.Count != 0)
            {
                this.MealsWithSum.ForEach(m => sb.Append(" ").Append(m.Meal.ToString()));
                sb.Append(" ");
            }
            return sb.ToString();
        }
    }

    public class ManagerOrder
    {
        public string Id { get; set; }
        public Manager Manager { get; set; }
        public DateTime CreatedTime { get; set; }
        public DateTime LastModifyTime { get; set; }
        public bool IsBooked { get; set; }
        public DateTime BookedTime { get; set; }
        public List<Order> Orders { get; set; }
        public List<MealWithSum> MealsWithSum
        {
            get
            {
                if (Orders != null && Orders.Count != 0)
                {
                    var mealWithTemplates = new List<MealWithSum>();
                    var groupedMeals = Orders.SelectMany(o => o.MealsWithSum).GroupBy(t => t.Meal.Id);
                    foreach (var groupedMeal in groupedMeals)
                    {
                        var tempMeal = groupedMeal.First();
                        var meal = new MealWithSum { Meal = tempMeal.Meal };
                        meal.MealSum = groupedMeal.Sum(m => m.MealSum);
                        mealWithTemplates.Add(meal);
                    }
                    return mealWithTemplates;
                }
                return null;
            }
        }
        public double TotalPrice
        {
            get
            {
                if (MealsWithSum != null && MealsWithSum.Count != 0)
                {
                    return MealsWithSum.Sum(t => t.MealSum * t.Meal.Price);
                }
                return 0;
            }
        }
    }
}
